<template>
  <div class="app-container usercenter">
    <div class="usercenter-header">
      <span class="header-title">集群：</span>
      <el-select v-model="selCluster" placeholder="请选择集群" @change="handleSelCluster">
        <el-option
          v-for="item in clusterOptions"
          :key="item.ip"
          :label="item.name"
          :value="item.name" />
      </el-select>
    </div>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        label="用户名">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.username }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="邮箱">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.email }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="状态">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.status ? '启用' : '禁用' }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="限制使用资源">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.isLimited ? '是' : '否' }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="开发者">
        <template slot-scope="scope">
          <div class="table-link">
            <span>{{ scope.row.developer ? '是' : '否' }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="注册时间">
        <template slot-scope="scope">
          <span>{{ new Date(scope.row.registeDate).Format("yyyy-MM-dd hh:mm:ss") }}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="basePagenate">
      <el-pagination
        :page-size="pageSize"
        :current-page="pageNumber"
        :total="totalPage"
        layout="prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
import { getClusterList } from '@/api/cluster'
import { getUserList } from '@/api/usercenter'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      selCluster: '',
      clusterOptions: [],
      // 总页数
      totalPage: 0,
      // 每页条数
      pageSize: 20,
      // 当前页
      pageNumber: 1
    }
  },
  created() {
    this._fetchClusterData()
  },
  methods: {
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.pageNumber = 1
      this._fetchUserData()
    },
    handleCurrentChange(val) {
      this.pageNumber = val
      this._fetchUserData()
    },
    // 点击table名称
    toDetail(row) {
      this.$store.dispatch('SaveCurCLuster', row)
      this.$router.push({ path: '/cluster/nodes' })
    },
    // 选择集群
    handleSelCluster(val) {
      this.list = []
      this._fetchUserData(val)
    },
    // 停止任务
    stopTask(row) {
      this.$confirm('此操作将停止该任务, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this._taskOperation(this.selCluster, row.name)
      })
    },
    // 查询集群列表
    _fetchClusterData() {
      this.listLoading = true
      getClusterList().then(res => {
        if (res.message.code === 0) {
          this.listLoading = false
          this.clusterOptions = res.data.clusters_info
          if (this.clusterOptions.length > 0) {
            this.selCluster = this.clusterOptions[0].name
            this._fetchUserData()
          }
        } else {
          this.$message({ message: res.message.message, showClose: true, type: 'error' })
        }
      })
    },
    // 查询用户列表
    _fetchUserData() {
      this.listLoading = true
      const param = `/${this.selCluster}?page=${this.pageNumber}&pageSize=${this.pageSize}&status=&isLimited=&searchKey=`
      getUserList(param).then(res => {
        if (res.code === 0) {
          this.listLoading = false
          this.list = res.result
          this.totalPage = res.count
        } else {
          this.$message({ message: res.msg, showClose: true, type: 'error' })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.usercenter {
  &-header {
    padding-bottom: 10px;
    .header-title {
      font-size: 14px;
      font-weight: 700;
    }
  }
  label {
    font-weight: normal
  }
  .table-action {
    color: #4285f4;
  }
  .table-link {
    &:hover {
      color: #4285f4;
    }
  }
}
</style>
